package cn.fsd.controller;

import cn.fsd.domain.Order;
import cn.fsd.dto.ExchangeOrderDTO;
import cn.fsd.dto.OrderDTO;
import cn.fsd.nacos.StoreRoomServer;
import cn.fsd.query.customer.OrderQuery;
import cn.fsd.query.delivery.PurchaseBean;
import cn.fsd.service.OrderService;
import cn.fsd.vo.OrderVO;
import cn.fsd.vo.ResultBean;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
@ResponseBody
public class OrderController {
    final OrderService orderService;
    final StoreRoomServer storeRoomServer;

    @Autowired
    public OrderController(OrderService orderService, StoreRoomServer storeRoomServer) {
        this.orderService = orderService;
        this.storeRoomServer = storeRoomServer;
    }

    @RequestMapping(value = "/getOrdersOfCustomer/{id}", method = RequestMethod.GET)
    @ApiOperation("获取一个客户所有订单信息")
    public ResultBean getOrdersOfCustomer(@PathVariable(value = "id") String id) {
        return ResultBean.success(orderService.getOrdersOfCustomer(id));
    }


    /**
     * 提交订单时调用，返回该订单是否缺货，需要通过dispatch远程调用
     *
     * @return true表示缺货
     */
    @RequestMapping(value = "/checkOrderOut", method = RequestMethod.POST)
    @ApiOperation(value = "提交订单时检查订单缺货")
    public ResultBean checkOrderOut(@RequestBody OrderDTO orderDTO) {
        return ResultBean.success(orderService.checkStockOutRemote(orderDTO));
    }

    /**
     * 确认缺货或不缺货后，调用该接口创建新订单<br/>
     * 缺货订单将统计缺货量，订单加入缺货订单队列，订单状态为“缺货”，<br/>
     * 有货订单将直接分配货物并挂起，订单状态为“已挂起”<br/>
     */
    @RequestMapping(value = "/newPurchaseOrder", method = RequestMethod.POST)
    @ApiOperation(value = "新订订单的接口")
    public ResultBean newPurchaseOrder(@RequestBody OrderDTO orderDTO) {
        orderService.newPurchaseOrder(orderDTO);
        return ResultBean.success();
    }

    /**
     * 创建换货订单
     */
    @RequestMapping(value = "/newExchangeOrder", method = RequestMethod.POST)
    @ApiOperation(value = "新建换货订单的接口")
    public ResultBean newExchangeOrder(@RequestBody ExchangeOrderDTO exchangeOrderDTO) {
        orderService.newExchangeOrder(exchangeOrderDTO);
        return ResultBean.success();
    }

    /**
     * 创建退货订单
     */
    @RequestMapping(value = "/newReturnOrder", method = RequestMethod.POST)
    @ApiOperation(value = "新建退货订单的接口")
    public ResultBean newReturnOrder(@RequestBody ExchangeOrderDTO exchangeOrderDTO) {
        orderService.newReturnOrder(exchangeOrderDTO);
        return ResultBean.success();
    }

    /**
     * 选择地址时，调用该接口获得最大库存量，即最大商品下单数量
     */
    @ApiOperation(value = "根据地址和商品获取下单的最大量限制")
    @RequestMapping(value = "/getMaxLimitation/{delivery_address}/{commodity_id}", method = RequestMethod.GET)
    public Integer getMaxLimitation(@PathVariable String delivery_address, @PathVariable String commodity_id) {
        return storeRoomServer.getMaxLimitation(delivery_address, commodity_id);
    }

    /**
     * 根据条件（起止日期，客户名称，订单类型）分页查询订单详情，包括客户和商品的全部信息
     *
     * @param orderQuery 客户名称，开始日期，结束日期，订单类型，以及分页数据
     */
    @ApiOperation(value = "查询所有订单信息")
    @RequestMapping(value = "/find", method = RequestMethod.POST)
    public ResultBean getOrders(@RequestBody OrderQuery orderQuery) {
        PageInfo<OrderVO> orderPageInfo = orderService.findVOByConditionByPage(orderQuery);
        return ResultBean.success(orderPageInfo);
    }

    /**
     * 用于修改订单状态。
     */
    @RequestMapping(value = "/changeOrderStatus/{order_id}/{status}", method = RequestMethod.POST)
    @ApiOperation("修改订单状态")
    public ResultBean changeOrderStatus(@PathVariable String order_id, @PathVariable String status) {
        orderService.changeOrderStatus(order_id, status);
        return ResultBean.success();
    }

    @RequestMapping(value = "/changeDeliveryStatus/{order_id}/{status}", method = RequestMethod.POST)
    @ApiOperation("修改订单配送状态")
    public ResultBean changeDeliveryStatus(@PathVariable String order_id, @PathVariable String status) {
        orderService.changeDeliveryStatus(order_id, status);
        return ResultBean.success();
    }


    /**
     * 退订订单，要求订单状态为 “缺货”或 “已挂起” 或“可分配”。<br/>
     * 退订后，订单状态变为 “已撤销”，订单类型变为“退订”，不再参与自动挂起
     */
    @ApiOperation(value = "退订订单的接口")
    @RequestMapping(value = "/cancelOrder/{order_id}", method = RequestMethod.POST)
    public ResultBean cancelOrder(@PathVariable String order_id) {
        orderService.cancelOrder(order_id);
        return ResultBean.success();
    }

    ///////////////////////// 为其他模块提供的接口 /////////////////////////
    @GetMapping("/getOrderById")
    @ApiOperation(value = "根据orderid获取对应的order")
    public Order getTheOrder(@RequestParam("orderId") String orderId) {
        return orderService.getOrderById(orderId);
    }

    // 获取购买量排行
    @GetMapping("/purchaseList")
    @ApiOperation("获取购买量排行")
    public List<PurchaseBean> getPurchaseList() {
        return orderService.getPurchaseList();
    }

    // 获取顾客总数量
    @GetMapping("/userNum")
    @ApiOperation("获取顾客总数量")
    public Integer getUserNum() {
        return orderService.getUserNum();
    }

}
